Passed
Push — master ( eee053...2b5061 )
by Joe Nilson
04:47
created

EditFacturaCliente.js ➔ businessDocViewSave   F

Complexity

Conditions 14

Size

Total Lines 91
Code Lines 68

Duplication

Lines 10
Ratio 10.99 %

Importance

Changes 0
Metric Value
cc 14
eloc 68
c 0
b 0
f 0
dl 10
loc 91
rs 3.5672

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like EditFacturaCliente.js ➔ businessDocViewSave often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
2
/*
3
 * This file is part of FacturaScripts - Dominican Republic Plugin
4
 * Copyright (C) 2013-2019 Carlos Garcia Gomez <[email protected]>
5
 * Copyright (C) 2019-2020 Joe Nilson <[email protected]>
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
function businessDocViewSubjectChanged()
21
{
22
    var data = {};
23
    $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
24
        data[value.name] = value.value;
25
    });
26
    data.action = "subject-changed";
27
    logConsole(data,"subject-changed");
28
29
    $.ajax({
30
        type: "POST",
31
        url: businessDocViewUrl,
32
        dataType: "json",
33
        data: data,
34
        success: function (results) {
35
            $("#doc_codpago").val(results.codpago);
36
            $("#doc_codserie").val(results.codserie);
37
            $("#formEditFacturaCliente select[name=ncftipopago]").val(results.ncftipopago);
38
39
            /**
40
             * Review the doc_codsubtipodoc existence,
41
             * if it exist we put the value from the customer data
42
             */
43
44
            if ($("#doc_codsubtipodoc").length !== 0) {
45
                $("#doc_codsubtipodoc").val(results.codsubtipodoc);
46
            }
47
            logConsole(results.codsubtipodoc,"codsubtipodoc");
48
            /**
49
             * Review the doc_codopersaciondoc existence,
50
             * if it exist we put the value from the customer data
51
             */
52
            if ($("#doc_codoperaciondoc").length !== 0) {
53
                $("#doc_codoperaciondoc").val(results.codoperaciondoc);
54
            }
55
            logConsole(results.codoperaciondoc,"codoperaciondoc");
56
            
57
            logConsole(results,"results");
58
59
            businessDocViewRecalculate();
60
        },
61
        error: function (xhr, status, error) {
62
            alert(xhr.responseText);
63
        }
64
    });
65
}
66
67
async function cargarTipoPago()
68
{
69
    return $.ajax({
70
        url: 'ListNCFTipoPago',
71
        async: true,
72
        data: {'action': 'busca_pago', 'tipopago': '01'},
73
        type: 'POST',
74
        datatype: 'json',
75
        success: function (response) {
76
            let data = JSON.parse(response);
77
            return data;
78
        },
79
        error: function (xhr, status) {
80
            alert('Ha ocurrido algún tipo de error ' + status);
81
        }
82
    });
83
}
84
85
async function cargarTipoMovimiento()
86
{
87
    return $.ajax({
88
        url: 'ListNCFTipoMovimiento',
89
        async: true,
90
        data: {'action': 'busca_movimiento', 'tipomovimiento': 'VEN'},
91
        type: 'POST',
92
        datatype: 'json',
93
        success: function (response) {
94
            let data = JSON.parse(response);
95
            return data;
96
        },
97
        error: function (xhr, status) {
98
            alert('Ha ocurrido algún tipo de error ' + status);
99
        }
100
    });
101
}
102
103
async function businessDocViewSave()
104
{
105
    $("#btn-document-save").prop("disabled", true);
106
    var tipoPago = await cargarTipoPago();
107
    var datosPago = JSON.parse(tipoPago);
108
    var ncfTipoPagoCliente = $("#formEditFacturaCliente select[name=ncftipopago]").val();
109
    var readOnlySelects = ($("#formSalesDocumentLine #doc_idestado").val() === '11');
110
    let selectOptionsPagos = "";
111
    $.each(datosPago.pagos, function (i, value) {
112
        let defaultSelected = ((value.codigo === '17' && ncfTipoPagoCliente === '') || ncfTipoPagoCliente === value.codigo) ? 'selected' : '';
113
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
114
        selectOptionsPagos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
115
    });
116
117
    var tipoMovimiento = await cargarTipoMovimiento();
118
    var datosMovimiento = JSON.parse(tipoMovimiento);
119
120
    let selectOptionsMovimientos = "";
121
    $.each(datosMovimiento.movimientos, function (i, value) {
122
        let defaultSelected = (value.codigo === '1') ? 'selected' : '';
123
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
124
        selectOptionsMovimientos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
125
    });
126
127
    let message = '<div class="form-content">\n' +
128
            '      <form class="form" role="form">\n' +
129
            '        <div class="form-group">\n' +
130
            '          <label for="ncftipopago">Tipo de Pago</label>\n' +
131
            '          <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' +
132
                        selectOptionsPagos +
133
            '          </select>\n' +
134
            '        </div>\n' +
135
            '        <div class="form-group">\n' +
136
            '          <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' +
137
            '          <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' +
138
                        selectOptionsMovimientos +
139
            '          </select>\n' +
140
            '        </div>\n' +
141
            '      </form>\n' +
142
            '    </div>';
143
144
    executeModal(
145
        'completeNCFData',
146
        'Complete la información faltante',
147
        message,
148
        'default',
149
        'saveBussinessDocument'
150
    );
151
152
153
    // bootbox.dialog({
154
    //     title: "Complete la información faltante",
155
    //     message: '<div class="form-content">\n' +
156
    //         '      <form class="form" role="form">\n' +
157
    //         '        <div class="form-group">\n' +
158
    //         '          <label for="ncftipopago">Tipo de Pago</label>\n' +
159
    //         '          <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' +
160
    //                     selectOptionsPagos +
161
    //         '          </select>\n' +
162
    //         '        </div>\n' +
163
    //         '        <div class="form-group">\n' +
164
    //         '          <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' +
165
    //         '          <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' +
166
    //                     selectOptionsMovimientos +
167
    //         '          </select>\n' +
168
    //         '        </div>\n' +
169
    //         '      </form>\n' +
170
    //         '    </div>',
171
    //     buttons: [
172
    //     {
173
    //         label: "Guardar",
174
    //         className: "btn btn-primary",
175
    //         callback: function () {
176
    //             var data = {};
177
    //             $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
178
    //                 data[value.name] = value.value;
179
    //             });
180
    //             data['ncftipopago'] = $('form #ncftipopago').val();
181
    //             data['ncftipomovimiento'] = $('form #ncftipomovimiento').val();
182
    //             data.action = "save-document";
183
    //             data.lines = getGridData();
184
    //
185
    //             $.ajax({
186
    //                 type: "POST",
187
    //                 url: businessDocViewUrl,
188
    //                 dataType: "text",
189
    //                 data: data,
190
    //                 success: function (results) {
191
    //                     if (results.substring(0, 3) === "OK:") {
192
    //                         $("#" + businessDocViewFormName + " :input[name=\"action\"]").val('save-ok');
193
    //                         $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit();
194
    //                     } else {
195
    //                         alert(results);
196
    //                         $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20));
197
    //                     }
198
    //                 },
199
    //                 error: function (msg) {
200
    //                     alert(msg.status + " " + msg.responseText);
201
    //                 }
202
    //             });
203
    //
204
    //         }
205
    //     },
206
    //     {
207
    //         label: "Cancelar",
208
    //         className: "btn btn-danger",
209
    //         callback: function () {
210
    //             return true;
211
    //         }
212
    //     }
213
    //     ],
214
    // });
215
216
    $("#btn-document-save").prop("disabled", false);
217
}
218
219
// $(document).ready(function () {
220
//     if ($("#doc_codsubtipodoc").val() !== '') {
221
//         verificarCorrelativoNCF($("#doc_codsubtipodoc").val());
222
//     }
223
//
224
//     $("#doc_codsubtipodoc").change( function() {
225
//         logConsole($("#doc_codsubtipodoc").val(),"#doc_codsubtipodoc val");
226
//         verificarCorrelativoNCF($("#doc_codsubtipodoc").val());
227
//
228
//     });
229
// });